package com.CST.controller.cb;


import com.CST.annotation.ResultObjectAnnotation;
import com.CST.common.api.Response;
import com.CST.common.api.ResultObject;
import com.CST.common.constant.DmlTypeConstant;
import com.CST.entity.cb.RuleDirectamortization;
import com.CST.entity.cb.selectInfo;
import com.CST.service.cb.IRuleDirectamortizationService;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * @author ly
 * @since 2021-10-27
 */
@Api(tags = {"定向分摊"})
@RestController
@RequestMapping("/ruleDirectamortization")

public class RuleDirectamortizationController {

    @Autowired
    private IRuleDirectamortizationService targetService;


    @PostMapping("/getDirectaAllocation")
    @ApiOperation("查询(分摊)")
    public ResultObject getDirectaAllocation(@RequestBody RuleDirectamortization entity) {
        List<RuleDirectamortization> result = targetService.getDirectaAllocation(entity);
        if (CollectionUtils.isEmpty(result)) {
            return ResultObject.successReturn(new ArrayList<RuleDirectamortization>());
        }
//        return ResultObject.successReturn(PageHelperTool.excutePageInfo(result, entity.getPageIndex(), entity.getPageSize()));
        return ResultObject.successReturn(result);
    }

    @PostMapping("/getDirectaAllocationById")
    @ApiOperation("获取定向分摊ByID")
    public ResultObject getDirectaAllocationById(@RequestBody RuleDirectamortization entity) {
        RuleDirectamortization result = targetService.getDirectaAllocationById(entity);
        return ResultObject.successReturn(result);
    }

    @PostMapping("/DeleteDirectaAllocation")
    @ApiOperation("删除(分摊)删除对应关系, 传id 多个以逗号隔开")
    @ResultObjectAnnotation(DmlTypeConstant.DELETE)
    public ResultObject DeleteDirectaAllocation(@RequestBody List<Integer> ids)  {
        if (ids == null || ids.stream().count() == 0) {
            return ResultObject.errorReturn("请选择要删除的记录");
        }
        Response result = targetService.batchDeleteDirectaAllocation(ids);
        return ResultObject.resultReturn(result);
    }

    @PostMapping("/addDirectaAllocation")
    @ApiOperation("添加科室部门对应数据,必传参数:ruleCode:规则edExtraFirstCode = 1的edCode,yard :1")
    public ResultObject addDirectaAllocation(@RequestBody RuleDirectamortization entity) {
        Response result = targetService.addDirectaAllocation(entity);
        if (entity.getDeptName() == null || entity.getTodeptname() == null) {
            return ResultObject.errorReturn("添加的源科室或者目标科室不能为空!");
        }
        return ResultObject.resultReturn(result);
    }

    @PostMapping("/insertBatchDirectaAllocationn")
    @ApiOperation("继承添加")
    public ResultObject insertBatchDirectaAllocationn(@RequestBody RuleDirectamortization entity) {

        Response result = targetService.insertBatchDirectaAllocationn(entity);
        return ResultObject.resultReturn(result);
    }

    @PostMapping("/modifyDirectaAllocation")
    @ApiOperation("修改科室部门对应数据,必传参数:ruleCode:规则edExtraFirstCode = 1的edCode,yard :1")
    public ResultObject modifyDirectaAllocation(@RequestBody RuleDirectamortization entity) {
        if (entity.getId() == null || entity.getId() == 0) {
            return ResultObject.errorReturn("未获取到记录ID!");
        }
        Response result = targetService.modifyDirectaAllocation(entity);
        return ResultObject.resultReturn(result);
    }

    @PostMapping("/getYearMonth")
    @ApiOperation("获取年月下拉数据")
    public ResultObject getYearMonth() {
        List<selectInfo> result = targetService.getYearMonth();
        if (CollectionUtils.isEmpty(result)) {
            return ResultObject.successReturn(new ArrayList<String>());
        }
        return ResultObject.successReturn(result);
    }


    @GetMapping("/getById/{id}")
    @ApiOperation("查看")
    @ResultObjectAnnotation(DmlTypeConstant.SELECT)
    public Object getById(@PathVariable String id) {
        return targetService.getById(id);
    }

}

